//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"OptionsState.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "OptionsState.h"

#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../Game.h"
#include <cassert>

OptionsState* OptionsState::GetInstance()
{
	static OptionsState Instance;

	return &Instance;
}
OptionsState::OptionsState()
{
	cursor = 0;
}
OptionsState::~OptionsState()
{
}
/*virtual*/ void OptionsState::Enter()
{
	titleImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MapStage3.png"));
	yUp = yDown = xRight = xLeft = false;
}
/*virtual*/ void OptionsState::Exit()
{

}
/*virtual*/ bool OptionsState::Input()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	if(DI->JoystickIsUnplugged())
	{
		if(DI->KeyPressed( DIK_DOWN) == true)
		{
			++cursor;

			if(cursor > 2)
				cursor = 2;
		}
		else if(DI->KeyPressed(DIK_UP) == true)
		{
			--cursor;

			if(cursor < 0)
				cursor = 0;
		}

		if(DI->KeyPressed(DIK_LEFT) == true)
		{
			switch(cursor)
			{
			case 0:
				{
					musVol -= 10.0;
					if(musVol < 0)
					{
						musVol = 0;
					}
				}
			case 1:
				{
					sfxVol -=10.0;
					if(sfxVol < 0)
					{
						sfxVol = 0;
					}
					//if(!CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
					//{
					//	CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
					//}
					CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVol/100);
				}
				break;
			}
		}
		else if(DI->KeyPressed(DIK_RIGHT) == true)
		{
			switch(cursor)
			{
			case 0:
				{
					musVol +=10.0;
					if(musVol > 100)
					{
						musVol = 100;
					}
					CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(musVol/100);
				}
				break;
			case 1:
				{
					sfxVol +=10.0;
					if(sfxVol > 100)
					{
						sfxVol = 100;
					}
					//if(!CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
					//{
					//	CSGD_XAudio2::GetInstance()->SFXPlaySound(Game::GetInstance()->)
					//}
					CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVol/100);
				}
				break;
			}
		}
		//DO FULLSCREEN
		if(DI->KeyPressed(DIK_RETURN) == true)
		{
			if(cursor == 2)
			{
				Game::GetInstance()->PopState();
			}
		}
		if(DI->KeyPressed(DIK_ESCAPE) == true)
		{
			Game::GetInstance()->PopState();
		}
	}
	else
	{
		Keyboard();
	}

	return true;

}
/*virtual*/ void OptionsState::Update( float fElapsedTime )
{

}
/*virtual*/ void OptionsState::Render()
{
	BitmapFont* Font = Game::GetInstance()->GetFont();
	CSGD_TextureManager::GetInstance()->Draw(titleImgID, 0, 0, 1.0f, 1.0f);

	TOSTRINGSTREAM t;
	t << musVol;

	Font->Draw( _T("OPTIONS"), 
		(int)((640 - (4*32*2.0f)) / 2), 
		100, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );


	Font->Draw( _T("VOLUME"), (500 - (4*32)) / 2, 300, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	Font->Draw(t.str().c_str(),  (500 - (4*32)) / 2+350, 300, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));

	t.str(_T(""));
	t << sfxVol;
	Font->Draw( _T("SFX VOLUME"), (500 - (4*32)) / 2, 350, 1.0f, D3DCOLOR_ARGB(255,255,255,255));
	Font->Draw(t.str().c_str(),  (500 - (4*32)) / 2+350, 350, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 0));	

	Font->Draw( _T("BACK"), (500 - (4*32)) / 2, 400, 1.0f, D3DCOLOR_ARGB(255,255,255,255));

	Font->Draw( _T("(     \t\t\t  )"), (500 - (7*32)) / 2, 300 + (cursor * 50), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

}
void OptionsState::SaveSoundInfo() const
{
	//ofstream fout("soundInfo.txt");
	//if(fout.is_open())
	//{
	//	fout << musVol << "\n" << sfxVol;
	//	fout.close();
	//}
	//fout.close();
}
void OptionsState::LoadSoundInfo()
{
	//ifstream fin("soundInfo.txt");
	//if(fin.is_open())
	//{
	//	fin >> musVol >> sfxVol;
	//}
	//fin.close();

	//CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(musVol/100);
	//CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVol/100);
}
bool OptionsState::Keyboard()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	if(DI->JoystickGetLStickYNormalized() > 0 && yDown == false)
	{
		++cursor;

		if(cursor > 2)
			cursor = 2;
			yDown = true;
	}
	else if(DI->JoystickGetLStickYNormalized() < 0 && yUp == false)
	{
		--cursor;

		if(cursor < 0)
			cursor = 0;
				yUp = true;
	}

	if(DI->JoystickGetLStickXNormalized() < 0 && xLeft == false)
	{
		switch(cursor)
		{
		case 0:
			{
				musVol -= 10.0;
				if(musVol < 0)
				{
					musVol = 0;
				}
			}
		case 1:
			{
				sfxVol -=10.0;
				if(sfxVol < 0)
				{
					sfxVol = 0;
				}
				//if(!CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
				//{
				//	CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
				//}
				CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVol/100);
			}
			break;
		}
		xLeft = true;
	}
	else if(DI->JoystickGetLStickXNormalized() > 0 && xRight == false)
	{
		switch(cursor)
		{
		case 0:
			{
				musVol +=10.0;
				if(musVol > 100)
				{
					musVol = 100;
				}
				CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(musVol/100);
			}
			break;
		case 1:
			{
				sfxVol +=10.0;
				if(sfxVol > 100)
				{
					sfxVol = 100;
				}
				//if(!CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(Game::GetInstance()->)
				//{
				//	CSGD_XAudio2::GetInstance()->SFXPlaySound(Game::GetInstance()->)
				//}
				CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(sfxVol/100);
			}
			break;
		}
		xRight = true;
	}

	if(DI->JoystickButtonPressed(0) == true)
	{
		if(cursor == 2)
		{
			Game::GetInstance()->PopState();
		}
	}
	if(DI->JoystickButtonPressed(1) == true)
	{
		Game::GetInstance()->PopState();
	}
	if(DI->JoystickGetLStickYNormalized() == 0)
	{
		yUp = false;
		yDown = false;
	}
	if(DI->JoystickGetLStickXNormalized() == 0)
	{
		xRight = false;
		xLeft = false;
	}
	return true;
}